#include "AdventurePart.h"
#include "Player.h"
#include "PlayerMgr.h"
#include "ProtoBuffer/AllProtocol.h"
#include "Character/monster/Monster.h"
#include "Adventure/AdventureMgr.h"
#include "Character/MissionPart.h"
#include "Mission/MissionManager.h"


using namespace ProtoBuf;

AdventurePart::AdventurePart() :m_adventureId(0), m_adventureTime(0)
{
}

AdventurePart::~AdventurePart()
{
}

bool AdventurePart::Init(Creature *pMaster, uint32_t partType, CharacterDBInfo* pCharacterDB /* = nullptr */)
{
	Part::Init(pMaster, partType);

	if (pCharacterDB->has_adventure())
	{
		const CharacterDBAdventureInfo &proto = pCharacterDB->adventure();
		m_adventureId = proto.adventure_id();
		m_adventureTime = proto.adventure_time();
		CheckAdventureTime();
	}

	g_GetEvent()->Subscribe(this, EVENT_CREATURE_DEAD, m_pMaster->GetCid(), CREATURE_MONSTER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_EQUIP_REFINE,m_pMaster->GetCid() , CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_TREASURE, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_ENTER_DUPLICATE, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_ENTER_SCENE, 0, CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_ITEM_USE, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_ITEM_CHANGE, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_CAMP_CHANGE, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_CAMP_ELECTION, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_DEPUTY_COLLECT_OPERATE, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_DEPUTY_FORGE_EQUIP, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_DEPUTY_MAKE_OPERATE, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_CLEAR_POING, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");	
	g_GetEvent()->Subscribe(this, EVENT_PAY, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_CREATURE_REVIVE, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_ATTEND_ACTIVITY, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_FINISH_ACTIVITY, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	g_GetEvent()->Subscribe(this, EVENT_CHAT, m_pMaster->GetCid(), CREATURE_PLAYER, "AdventurePart");
	
	

	return true;
}

bool AdventurePart::UnInit()
{
	g_GetEvent()->UnSubscribeAll(this);
	m_adventureId = 0;
	m_adventureTime = 0;

	return true;
}

bool AdventurePart::SaveDB(CharacterDBInfo& characterDB)
{
	CharacterDBAdventureInfo*proto = characterDB.mutable_adventure();
	if (nullptr != proto)
	{
		proto->set_adventure_id(m_adventureId);
		proto->set_adventure_time(m_adventureTime);
	}
	return true;
}


bool AdventurePart::OnMsgHandle(uint32_t cmd, const char* data, uint32_t len)
{
	switch (cmd)
	{
	case CLIENT_TO_LOGIC_ADVENTURE_INFO:
		{
			Info(data, len);
		}
		break;
	case CLIENT_TO_LOGIC_ADVENTURE_ACCEPT:
		{
			Accept(data, len);
		}
		break;
	default:
		break;
	}
	return true;
}


//奇遇信息
void AdventurePart::Info(const char *data, uint32_t len)
{
	CheckAdventureTime();
	Adventure_InfoRsp rsp;
	AdventureProto *proto = rsp.mutable_info();
	if (nullptr != proto)
	{
		proto->set_adventure_id(m_adventureId);
		proto->set_adventure_time(m_adventureTime);
	}
	m_pMaster->SendDataToClient(EMODULE_ID_ADVENTURE, LOGIC_TO_CLIENT_ADVENTURE_INFO, &rsp);
}
//通知奇遇信息
void AdventurePart::NotifyInfo()
{
	CheckAdventureTime();
	Adventure_UpdateRsp rsp;
	AdventureProto *proto = rsp.mutable_update();
	if (nullptr != proto)
	{
		proto->set_adventure_id(m_adventureId);
		proto->set_adventure_time(m_adventureTime);
	}
	m_pMaster->SendDataToClient(EMODULE_ID_ADVENTURE, LOGIC_TO_CLIENT_ADVENTURE_UPDATE, &rsp);
}
//接取奇遇任务
void AdventurePart::Accept(const char *data, uint32_t len)
{
	Adventure_AcceptReq req;
	if (!req.ParseFromArray(data,len))
	{
		LogErrFmtPrint("AdventurePart::Accept...Adventure_AcceptReq parse failed...");
		return;
	}
	int32_t itype = req.type();
	if (itype != 1 && itype != 2) //1 接受， 2 拒绝
	{
		LogErrFmtPrint("AdventurePart::Accept...itype != 1 && itype != 2...cid:%lu,itype:%d ", m_pMaster->GetCid(),itype);
		return;
	}
	CheckAdventureTime();
	Adventure_AcceptRsp rsp;
	if (m_adventureId <= 0)
	{
		LogErrFmtPrint("AdventurePart::Accept...m_adventureId <= 0...cid:%lu ", m_pMaster->GetCid());
		return;
	}
	if (m_adventureTime + g_GetAdventureMgr()->AdventureAcceptTime() <= g_GetGlobalServerTime()->UnixSec())
	{
		//超时
		rsp.set_retcode(RET_ADVENTURE_OVER_TIME);
	}
	else
	{
		const AdventureCfgInfo *pAdventureCfg = g_GetAdventureMgr()->GetAdventureCfgInfo(m_adventureId);
		if (nullptr == pAdventureCfg)
		{
			LogErrFmtPrint("AdventurePart::Accept...nullptr == pAdventureCfg..cid:%lu ,m_adventureId:%u ", m_pMaster->GetCid(), m_adventureId);
			return;
		}
		if (1 == itype)
		{
			MissionPart *pMissionPart = dynamic_cast<MissionPart*>(m_pMaster->GetPart(PART_MISSION));
			if (nullptr == pMissionPart)
			{
				LogErrFmtPrint("AdventurePart::Accept...nullptr == pMissionPart..cid:%lu ,m_adventureId:%u ", m_pMaster->GetCid(), m_adventureId);
				return;
			}
			int32_t ret = pMissionPart->AcceptMissionByAdventure(pAdventureCfg->chapterId);
			if (RET_SUCCESS == ret)
			{
				//接取成功,重置奇遇ID和奇遇时间
				m_adventureId = 0;
				m_adventureTime = 0;
				//
				rsp.set_retcode(RET_SUCCESS);

			}
			else
			{
				rsp.set_retcode(RET_FAIL);
				LogErrFmtPrint("AdventurePart::Accept...can not accept adventure mission..cid:%lu ,m_adventureId:%u,chapterid:%u,ret:%d ", m_pMaster->GetCid(), m_adventureId, pAdventureCfg->chapterId, ret);
			}
		}
		else
		{
			//拒绝,重置奇遇ID和奇遇时间
			m_adventureId = 0;
			m_adventureTime = 0;
			rsp.set_retcode(RET_SUCCESS);
		}
		
	}
	m_pMaster->SendDataToClient(EMODULE_ID_ADVENTURE, LOGIC_TO_CLIENT_ADVENTURE_ACCEPT, &rsp);
}

void AdventurePart::CheckAdventureTime()
{
	if (m_adventureId > 0)
	{
		if (m_adventureTime + g_GetAdventureMgr()->AdventureAcceptTime() <= g_GetGlobalServerTime()->UnixSec())
		{
			m_adventureId = 0;
			m_adventureTime = 0;
		}		
	}
}


void AdventurePart::OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	switch (nEventID)
	{
	case EVENT_CREATURE_DEAD:
		{
			DieEvent dieEvent = *(DieEvent*)pContext;
			if (dieEvent.monsterId > 0)
			{
				OnMonsDieEvent(dieEvent);
			}			
		}
		break;
	case EVENT_CREATURE_REVIVE:
		{
			ReliveEvent reliveEvent = *(ReliveEvent*)pContext;
			OnReliveEvent(reliveEvent);
		}
		break;
	case EVENT_EQUIP_REFINE:
		{
			EquipRefineEvent refineEvent = *(EquipRefineEvent*)pContext;
			OnRefineEvent(refineEvent);
		}
		break;
	case EVENT_TREASURE:
		{
			TreasureEvent treasureEvent = *(TreasureEvent*)pContext;
			OnTreasureEvent(treasureEvent);
		}
		break;
	case EVENT_ENTER_DUPLICATE:
		{
			EnterDupEvent enterEvent = *(EnterDupEvent*)pContext;
			OnEnterDupEvent(enterEvent);
		}
		break;
	case EVENT_ITEM_USE:
		{
			ItemUseEvent useEvent = *(ItemUseEvent*)pContext;
			OnItemUseEvent(useEvent);
		}
		break;
	case EVENT_ITEM_CHANGE:
		{
			ItemChangeEvent changeEvent = *(ItemChangeEvent*)pContext;
			OnItemChangeEvent(changeEvent);
		}
		break;
	case EVENT_CAMP_CHANGE:
		{
			ChangeCampEvent changeEvent = *(ChangeCampEvent*)pContext;
			OnCampChangeEvent(changeEvent);
		}
		break;
	case EVENT_CAMP_ELECTION:
		{
			CampElectionEvent selEvent = *(CampElectionEvent*)pContext;
			OnCampSelectEvent(selEvent);
		}
		break;
	case EVENT_DEPUTY_COLLECT_OPERATE:
		{
			CollectOperateEvent sucessEvent = *(CollectOperateEvent*)pContext;
			OnCollectSucessEvent(sucessEvent);
		}
		break;
	case EVENT_DEPUTY_FORGE_EQUIP:
		{
			ForgeEquipEvent forgeEvent = *(ForgeEquipEvent*)pContext;
			OnMakeEvent(forgeEvent);
		}
		break;
	case EVENT_DEPUTY_MAKE_OPERATE:
		{
			MakeOperateEvent operateEvent = *(MakeOperateEvent*)pContext;
			OnMakeItemEvent(operateEvent);
		}
		break;
	case EVENT_PAY:
		{
			PayEvent payEvent = *(PayEvent*)pContext;
			OnChargeEvent(payEvent);
		}
		break;
	case EVENT_ATTEND_ACTIVITY:
		{
			AttendActEvent attendEvent = *(AttendActEvent*)pContext;
			OnAttendActEvent(attendEvent);
		}
		break;
	case EVENT_FINISH_ACTIVITY:
		{
			FinishActEvent finishEvent = *(FinishActEvent*)pContext;
			OnFinishActEvent(finishEvent);
		}
		break;
	case EVENT_ENTER_SCENE:
		{
			EnterSceneEvent enterEvent = *(EnterSceneEvent*)pContext;
			OnEnterSceneEvent(enterEvent);
		}
		break;
	case EVENT_CLEAR_POING:
		{
			ClearPointEvent clearEvent = *(ClearPointEvent*)pContext;
			OnClearPointEvent(clearEvent);
		}
		break;
	case EVENT_CHAT:
		{
			ChatWorldEvent chatEvent = *(ChatWorldEvent*)pContext;
			OnWorldChatEvent(chatEvent);
		}
		break;
	}
}

/////////////////////////////////// 系统事件 ///////////////////////////////////////

//怪物死亡
void AdventurePart::OnMonsDieEvent(DieEvent &dieEvent)
{
	if (dieEvent.killerCid != m_pMaster->GetCid())
	{
		return;
	}
	CharIDType selfCid = m_pMaster->GetCid();
	//击杀怪物
	KillMons(dieEvent.monsterId);
	//怪物计数
	MonsDieCount(dieEvent.monsterId);
	//
	Scene *pScene = m_pMaster->GetScene();
	if (nullptr == pScene)
	{
		return;
	}
	Monster *pMonster = pScene->GetMonster(dieEvent.nCid);
	if (nullptr == pMonster)
	{
		return;
	}
	//Point3<float> pos = pMonster->GetPos();
	//统计仇恨列表和归属列表中组队的玩家，可能会有重复部分
	SET_UINT64 setTarget;
	setTarget.clear();
	//
	VEC_UINT64::iterator iterHate = dieEvent.hateTargetList.begin();
	for (; iterHate != dieEvent.hateTargetList.end(); ++iterHate)
	{
		setTarget.insert((*iterHate));
	}
	ORDER_MAP_UINT32_VEC_UINT64::iterator iterTeam = dieEvent.mapTargetTeamMem.begin();
	for (; iterTeam != dieEvent.mapTargetTeamMem.end(); ++iterTeam)
	{
		VEC_UINT64 &vecMem = iterTeam->second;
		VEC_UINT64::iterator iterMem = vecMem.begin();
		for (; iterMem != vecMem.end(); ++iterMem)
		{
			setTarget.insert((*iterMem));
		}
	}
	//任务杀怪计数
	SET_UINT64::iterator iterTarget = setTarget.begin();
	for (; iterTarget != setTarget.end(); ++iterTarget)
	{
		CharIDType shareCid = (*iterTarget);
		//过滤自己
		if (selfCid == shareCid)
		{
			continue;
		}
		Player* pPlayer = g_GetCreatureMgr()->GetPlayer(shareCid);
		if (nullptr != pPlayer)
		{
			AdventurePart *pAdventurePart = dynamic_cast<AdventurePart*>(pPlayer->GetPart(PART_ADVENTURE));
			if (nullptr != pAdventurePart)
			{
				pAdventurePart->ShareKillMonsCount(dieEvent.monsterId);
			}
		}
	}
}

//共享杀怪计数
void AdventurePart::ShareKillMonsCount(uint32_t monsId)
{
	MonsDieCount(monsId);
}
//客户端行为
bool AdventurePart::OnClientAction(int32_t actionType, uint32_t actionVal)
{
	//进入区域，离开区域，NPC交谈
	ActionCfgInfo action;
	action.actionType = actionType;
	action.actionVal = actionVal;
	return CheckTriggerAdventure(action);
}
/*

//进入区域
bool AdventurePart::OnEnterArea(uint32_t areaId)
{
	return EnterArea(areaId);
}
//离开区域
bool AdventurePart::OnLeaveArea(uint32_t areaId)
{
	LeaveArea(areaId);
}
//NPC交谈
bool AdventurePart::OnNpcTalk(uint32_t npcId)
{
	return NpcTalk(npcId);
}
*/

//精炼
void AdventurePart::OnRefineEvent(EquipRefineEvent &refineEvent)
{
	//奇遇 精炼事件
	Refine();
}

//复活
void AdventurePart::OnReliveEvent(ReliveEvent &reliveEvent)
{
	//奇遇 复活事件
	Relive();
}

//挖宝
void AdventurePart::OnTreasureEvent(TreasureEvent &treasureEvent)
{

	//奇遇 挖宝事件
	Treasure();
}

//进入副本
void AdventurePart::OnEnterDupEvent(EnterDupEvent &enterEvent)
{
	EnterDup(enterEvent.dupId);
}
//使用物品
void AdventurePart::OnItemUseEvent(ItemUseEvent &useEvent)
{
	UseItem(useEvent.itemId);
}
//物品改变
void AdventurePart::OnItemChangeEvent(ItemChangeEvent &changeEvent)
{
	if (changeEvent.itemNum > 0)
	{
		AddItem(changeEvent.itemId);
	}
}
//转换阵营
void AdventurePart::OnCampChangeEvent(ChangeCampEvent &changeEvent)
{
	CampChange();
}
//首领竞选
void AdventurePart::OnCampSelectEvent(CampElectionEvent &selEvent)
{
	if (2 == selEvent.type)
	{
		CampSelect();
	}	
}
//采集成功
void AdventurePart::OnCollectSucessEvent(CollectOperateEvent &sucessEvent)
{
	Collect(sucessEvent.type);
}

//制造
void AdventurePart::OnMakeEvent(ForgeEquipEvent &forgeEvent)
{
	Make((int32_t)forgeEvent.type);
}
//制造道具
void AdventurePart::OnMakeItemEvent(MakeOperateEvent &operateEvent)
{
	MakeItem((int32_t)operateEvent.type);
}

//充值
void AdventurePart::OnChargeEvent(PayEvent &payEvent)
{
	Charge();
}

//参加活动
void AdventurePart::OnAttendActEvent(AttendActEvent &attendEvent)
{
	AttendActivity(attendEvent.activityId);
}
//完成活动
void AdventurePart::OnFinishActEvent(FinishActEvent &finishEvent)
{
	FinishActivity(finishEvent.activityId);
}

//切换场景
void AdventurePart::OnEnterSceneEvent(EnterSceneEvent &enterEvent)
{
	if (enterEvent.cId == m_pMaster->GetCid() && ETransType_Pandos == enterEvent.transParam.transType)
	{
		PandosTrans();
	}
}
//洗点
void AdventurePart::OnClearPointEvent(ClearPointEvent &clearEvnet)
{
	ClearPoint();
}

//世界聊天
void AdventurePart::OnWorldChatEvent(ChatWorldEvent &chatEvent)
{
	WorldChat();
}


///////////////////////////////// 奇遇行为事件 /////////////////////////////////////////

//进入区域
bool AdventurePart::EnterArea(uint32_t areaId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_EnterArea;
	action.actionVal = areaId;
	return CheckTriggerAdventure(action);
}
//离开区域
bool AdventurePart::LeaveArea(uint32_t areaId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_LeaveArea;
	action.actionVal = areaId;
	return CheckTriggerAdventure(action);
}
//进入副本
void AdventurePart::EnterDup(uint32_t dupId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_EnterDup;
	action.actionVal = dupId;
	CheckTriggerAdventure(action);
}
//使用物品
void AdventurePart::UseItem(uint32_t itemId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_ItemUse;
	action.actionVal = itemId;
	CheckTriggerAdventure(action);
}
//获得物品
void AdventurePart::AddItem(uint32_t itemId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_ItemAdd;
	action.actionVal = itemId;
	CheckTriggerAdventure(action);
}
//NPC交谈
bool AdventurePart::NpcTalk(uint32_t npcId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_NpcTalk;
	action.actionVal = npcId;
	return CheckTriggerAdventure(action);
}
//转换阵营
void AdventurePart::CampChange()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_CampChange;
	action.actionVal = m_pMaster->GetAttr(C_CAMP_ID);
	CheckTriggerAdventure(action);
}

//完成采集 sonType:采集子类型
void AdventurePart::Collect(int32_t sonType)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_Collect;
	action.actionVal = sonType;
	CheckTriggerAdventure(action);
}
//完成制造
void AdventurePart::Make(int32_t sonType)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_Make;
	action.actionVal = sonType;
	CheckTriggerAdventure(action);
}
//完成制造道具
void AdventurePart::MakeItem(int32_t sonType)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_Make;
	action.actionVal = sonType;
	CheckTriggerAdventure(action);
}
//击杀怪物
void AdventurePart::KillMons(uint32_t monsId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_KillMons;
	action.actionVal = monsId;
	CheckTriggerAdventure(action);
}
//怪物计数
void AdventurePart::MonsDieCount(uint32_t monsId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_MonsCount;
	action.actionVal = monsId;
	CheckTriggerAdventure(action);
}
//玩家复活
void AdventurePart::Relive()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_Relive;
	CheckTriggerAdventure(action);
}
//世界喊话
void AdventurePart::WorldChat()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_WorldChat;
	CheckTriggerAdventure(action);
}
//发送邮件
void AdventurePart::SendMail()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_SendMail;
	CheckTriggerAdventure(action);
}
//完成洗点
void AdventurePart::ClearPoint()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_ClearPoint;
	CheckTriggerAdventure(action);
}
//精炼
void AdventurePart::Refine()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_Refine;
	CheckTriggerAdventure(action);
}
//星盘传送
void AdventurePart::PandosTrans()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_PandosTrans;
	CheckTriggerAdventure(action);
}
//激活坐骑
void AdventurePart::ActiveHorse(uint32_t horseId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_ActiveHorse;
	action.actionVal = horseId;
	CheckTriggerAdventure(action);
}
//参加活动
void AdventurePart::AttendActivity(uint32_t activityId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_AttendAct;
	action.actionVal = activityId;
	CheckTriggerAdventure(action);
}
//完成活动
void AdventurePart::FinishActivity(uint32_t activityId)
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_FinishAct;
	action.actionVal = activityId;
	CheckTriggerAdventure(action);
}
//参加竞选
void AdventurePart::CampSelect()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_CampSelect;
	CheckTriggerAdventure(action);
}
//挖宝
void AdventurePart::Treasure()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_Treasure;
	CheckTriggerAdventure(action);
}
//充值
void AdventurePart::Charge()
{
	ActionCfgInfo action;
	action.actionType = EAdventureAction_Charge;
	CheckTriggerAdventure(action);
}

//检查触发奇遇
bool AdventurePart::CheckTriggerAdventure(ActionCfgInfo &action)
{
	CheckAdventureTime();
	if (m_adventureId > 0)
	{
		//同时只能触发一个奇遇
		return false;
	}
	Player *pPlayer = dynamic_cast<Player*>(m_pMaster);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("AdventurePart::CheckTriggerAdventure...nullptr == pPlayer...cid:%lu,actionType:%d ", m_pMaster->GetCid(), action.actionType);
		return false;
	}
	MissionPart *pMissionPart = dynamic_cast<MissionPart*>(pPlayer->GetPart(PART_MISSION));
	if (nullptr == pMissionPart)
	{
		LogErrFmtPrint("AdventurePart::CheckTriggerAdventure...nullptr == pMissionPart...cid:%lu,actionType:%d ", m_pMaster->GetCid(), action.actionType);
		return false;
	}
	if (pMissionPart->MissionNumByType((int32_t)MISSION_TYPE_ID_ADVENTURE) > 0)
	{
		//已经接取了一个奇遇任务，不能触发奇遇了
		return false;
	}
	const SET_UINT32 *pSetAdventure = g_GetAdventureMgr()->GetAdventureByAction(action.actionType);
	if (nullptr == pSetAdventure)
	{
		return false;
	}
	bool bFlag = false;
	SET_UINT32::const_iterator iter = pSetAdventure->begin();
	for (; iter != pSetAdventure->end(); ++iter)
	{
		uint32_t adventureId = (*iter);
		const AdventureCfgInfo *pAdventureCfgInfo = g_GetAdventureMgr()->GetAdventureCfgInfo(adventureId);
		if (nullptr == pAdventureCfgInfo)
		{
			continue;
		}
		//比较行为和行为参数
		if ((pAdventureCfgInfo->action.actionType != action.actionType)
			|| (pAdventureCfgInfo->action.actionVal != action.actionVal)
			|| (pAdventureCfgInfo->action.param1 != action.param1)
			|| (pAdventureCfgInfo->action.param2 != action.param2)
			)
		{
			continue;
		}
		if (pMissionPart->HaveChapterSubmit(pAdventureCfgInfo->chapterId)
			|| pMissionPart->HaveAcceptChapter(pAdventureCfgInfo->chapterId)
			)
		{
			continue;
		}
		const MissionChapterInfo *pChapterCfg = g_GetMissionManager()->GetMissionChapterCfgInfo(pAdventureCfgInfo->chapterId);
		if (nullptr == pChapterCfg)
		{
			LogErrFmtPrint("AdventurePart::CheckTriggerAdventure...nullptr == pChapterCfg..cid:%lu,adventureid:%u,actionType:%u,chapterid:%u ", m_pMaster->GetCid(), adventureId, action.actionType, pChapterCfg->chapterId);
			continue;
		}
		SCanAcceptParam param;
		param.chapterId = pChapterCfg->chapterId;
		param.missionId = pChapterCfg->firstMissionId;
		int32_t ret = g_GetMissionManager()->CanAccept(pPlayer, pAdventureCfgInfo->accept, param);
		if (RET_SUCCESS == ret)
		{
			//可接取
			m_adventureId = adventureId;
			m_adventureTime = g_GetGlobalServerTime()->UnixSec();
			//通知前端
			NotifyInfo();
			//
			bFlag = true;
			//
			LogDebugFmtPrint("AdventurePart::CheckTriggerAdventure....cid:%lu,adventureid:%u,time:%lu,actiontype:%u,actionVal:%d,param1:%d,param2:%d ", m_pMaster->GetCid(), m_adventureId, m_adventureTime, action.actionType, action.actionVal, action.param1, action.param2);
			//跳出循环
			break;
		}
		else
		{
			LogErrFmtPrint("AdventurePart::CheckTriggerAdventure...nullptr == pChapterCfg..cid:%lu,adventureid:%u,actionType:%u,chapterid:%u,camp:%d,ret:%d ", m_pMaster->GetCid(), adventureId, action.actionType, pChapterCfg->chapterId,m_pMaster->GetAttr(C_CAMP_ID),ret);
		}
	}
	return bFlag;
}